package com.wang.initializer;

import com.wang.config.AppConfig;
import org.springframework.util.CollectionUtils;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by wangq on 2021/8/7.
 */
public class MySpringServletContainerInitializer implements ServletContainerInitializer {
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        List<WebApplicationInitializer> initializers = new ArrayList<WebApplicationInitializer>();
        if (!CollectionUtils.isEmpty(set)) {
            for (Class<?> webappInit : set) {
                if (!webappInit.isInterface() && !Modifier.isAbstract(webappInit.getModifiers()) &&
                        WebApplicationInitializer.class.isAssignableFrom(webappInit)) {
                    try {
                        initializers.add((WebApplicationInitializer) webappInit.newInstance());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            if (!CollectionUtils.isEmpty(initializers)) {
                for (WebApplicationInitializer initializer : initializers) {
                    initializer.onStartup(servletContext);
                }
            }
        }

    }
}

